package cn.cvs.controller;


import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import cn.cvs.service.sysRole.SysRoleService;
import com.alibaba.fastjson.JSON;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/sys/role/")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 下拉框同步角色列表
     */
    @ResponseBody
    @GetMapping(value = "/simpleList", produces = {"aplication/json;charset=UTF-8"})
    public Object simpleList() throws Exception {
        System.out.println("进入查询角色列表方法");
        List<SysRole> sysRoles = sysRoleService.findAllSysRole();
        return JSON.toJSONString(sysRoles);
    }

    /**
     *查询用户数据
     */
    @RequestMapping("/list")
    public String list(HttpSession session, Model model) throws Exception {
            List<SysRole> allSysRole = sysRoleService.findAllSysRole();
            model.addAttribute("roleList",allSysRole);
            return "sysRole/list";

    }

    /**
     * 跳转新增页面
     */
    @RequestMapping("/toAdd")
    public String toAdd(HttpSession session){
            return "sysRole/add";
    }

    /**
     * Ajax验证角色编码
     */
    @ResponseBody
    @GetMapping("/codeExist")
    public Object codeExist(@RequestParam String code) throws Exception {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        System.out.println("进入Ajax验证角色编码，code参数：" + code);
        if (StringUtils.isNullOrEmpty(code)) {
            resultMap.put("exist", -1);
        } else {
            SysRole sysRole = sysRoleService.findSysRoleBycode(code);
            if (sysRole != null) {
                resultMap.put("exist", 1);//账号已存在
            } else {
                resultMap.put("exist", 0);//账号可用
            }
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public String add(SysRole sysRole,HttpSession session) throws Exception {
            SysUser user = (SysUser) session.getAttribute("userSession");
            sysRole.setCreatedUserId(user.getId());
            sysRole.setCreatedTime(new Date());
            boolean flag = sysRoleService.addSysRole(sysRole);
            //判断是否添加成功
            if (flag) {
                return "redirect:/sys/role/list";
            } else {
                return "sysRole/add";
        }
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable String id,Model model) throws Exception {
        SysRole sysRole = sysRoleService.findSysRoleById(id);
        model.addAttribute("sysRole",sysRole);
        return "sysRole/update";
    }

    @PostMapping("/update")
    public String update(SysRole sysRole,HttpSession session) throws Exception {
        SysUser sysUser = (SysUser) session.getAttribute("userSession");
        sysRole.setUpdatedTime(new Date());
        sysRole.setUpdatedUserId(sysUser.getId());
        boolean flag = sysRoleService.updateSysRole(sysRole);
        if (flag){
            return "redirect:/sys/role/list";
        }else {
            return "redirect:/sys/role/toUpdate/{id}";
        }
    }

    /**
     * Ajax删除供货商信息
     */
    @ResponseBody
    @DeleteMapping("/del/{id}")
    public Object del(@PathVariable String id) throws Exception {
        System.out.println("进入删除角色信息方法，参数"+id);
        HashMap<String,Object> resultMap = new HashMap<String, Object>();
        SysRole sysRole = sysRoleService.findSysRoleById(id);
        if (sysRole == null){//判断该用户是否存在
            resultMap.put("delResult","notexist");
        }else {
            if (sysRoleService.delete(id)){//判断是否删除成功
                resultMap.put("delResult","true");
            }else {
                resultMap.put("delResult","false");
            }
        }
        return JSON.toJSONString(resultMap);
    }
}
